//
//  AppDelegate.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/4.
//

import UIKit
import IQKeyboardManager
import DCURLRouter
@main

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    @available(iOS 13.0, *)
    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let normalAttrs = [NSAttributedString.Key.foregroundColor:rgba(hex: "#989FAD"),NSAttributedString.Key.font:SLFontConfig.medium11]
        let selectedAttrs = [NSAttributedString.Key.foregroundColor:rgba(hex: "#1608EC"),NSAttributedString.Key.font:SLFontConfig.medium11]
        let tabBar = UITabBarItem.appearance()
        tabBar.setTitleTextAttributes(normalAttrs, for: .normal)
        tabBar.setTitleTextAttributes(selectedAttrs, for: .selected)
        
        IQKeyboardManager.shared().isEnabled = true
        IQKeyboardManager.shared().isEnableAutoToolbar = false
        IQKeyboardManager.shared().shouldPlayInputClicks = true
        IQKeyboardManager.shared().shouldResignOnTouchOutside = true
        IQKeyboardManager.shared().toolbarManageBehavior = .byTag
        IQKeyboardManager.shared().previousNextDisplayMode = .alwaysShow

        if window == nil {
            window = UIWindow(frame: UIScreen.main.bounds)
        }
        let startVC = SLStartPage()
        window?.backgroundColor = .white
        window?.rootViewController = startVC
        window?.makeKeyAndVisible()
        
        
        //:清除app角标
        UIApplication.shared.applicationIconBadgeNumber = 0
        return true
    }
    //ios 9.0以前推荐使用
    func application(_ application: UIApplication, handleOpen url : URL) -> Bool{
        let urlString = url.absoluteString
        if urlString.contains("fon://"){
            self.jump(urlStr: urlString)
        }else if urlString.contains("http"){
            self.jump(urlStr: urlString)
            
        }
        return false
   }
    //ios 9.0及以后推荐使用
    func application(_ app: UIApplication, open url: URL, options : [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool{
        let handleUrlStr = url.absoluteString
        if url.host == "safepay"{
         
        }
        if handleUrlStr.contains("fon://"){
            self.jump(urlStr: handleUrlStr)
        }
        print(url)
        return false
    }
    func showLogin(){
        DispatchQueue.main.async {
            // 在这里执行主线程上的任务
            let login = SLNavigationController(rootViewController: SLLoginVC())
            self.window?.rootViewController = login
        }
    }
    func showGuide(){
        DispatchQueue.main.async {
            let guide = SLNavigationController(rootViewController: SLGuideVC())
            self.window?.rootViewController = guide
        }
    }
    func jumpMainVC(){
        self.window?.rootViewController = self.tabBarController
    }
    
    func jump(urlStr: String) {
        
        if urlStr.contains("sul://"){
            //以下是跳转相关
            let mainTabBarVC:SLTabBarController = self.window?.rootViewController as! SLTabBarController
            if urlStr.contains("indoislamic"){//设置
               let settingPage = SLMineSettingVC()
                DCURLRouter.push(settingPage, animated: true)
            }else if urlStr.contains("rivers"){//首页
                mainTabBarVC.selectedIndex = 0
            }else if urlStr.contains("period"){//登录
                GoToLoginVC()
            }else if urlStr.contains("chhattisgarh"){//订单
                if !SLUserAccount.isLogin {
                    GoToLoginVC()
                    return
                }
                let arr = urlStr.components(separatedBy: "=")
                mainTabBarVC.selectedIndex = 1
                if let index = arr.last{
                    if let page = mainTabBarVC.viewControllers?[1] as? UINavigationController {
                        if let orderPage = page.viewControllers.first as? SLRecordVC{
                            orderPage.setSelectIndex(index: Int(index)!)
                        }
                    }
                }
            }else if urlStr.contains("imported"){//产品详情
                if !SLUserAccount.isLogin {
                    GoToLoginVC()
                    return
                }
                let arr = urlStr.components(separatedBy: "=")
                if let estimated = arr.last{
                    let orderDetail = SLProjectDetailVC(id: estimated)
                    DCURLRouter.push(orderDetail, animated: true)
                }
            }
        }
        if urlStr.contains("http"){//网络链接跳转
            let webVC = SLWebVC()
            webVC.loadUrl(url:urlStr)
            DCURLRouter.push(webVC, animated: true)
        }
        
    }
     let tabBarController: SLTabBarController = {
       let tabbarV = SLTabBarController()
        return tabbarV
    }()


}

